<?php
namespace TennisTournament\Service;

use TennisTournament\Model\Result;
use TennisTournament\Model\WinnerEnum;
use Zend\Json\Json;

class ResultServiceImpl implements ResultService {
    /**
     * Get all taken scores for the specific {@link Match}
     * @param array $results from {@link Result}
     * @param $hostOrGuest value from {@link WinnerEnum}
     * @return integer
     */
    public function getAllTakenScores(array $results, $hostOrGuest) {
        $allTakenScores = 0;

        /** @var $result Result */
        foreach($results as $result) {
            if ($hostOrGuest == WinnerEnum::HOST) {
                $allTakenScores += $result->getHostScore();
            } else {
                $allTakenScores += $result->getGuestScore();
            }
        }
    }

    /**
     * Get all get scores for the specific {@link Match}
     * @param array $results from {@link Result}
     * @param $hostOrGuest value from {@link WinnerEnum}
     * @return integer
     */
    public function getAllGetScores(array $results, $hostOrGuest) {
        $allGetScores = 0;

        /** @var $result Result */
        foreach($results as $result) {
            if ($hostOrGuest == WinnerEnum::HOST) {
                $allGetScores += $result->getGuestScore();
            } else {
                $allGetScores += $result->getHostScore();
            }
        }
    }

    /**
     * Encode a Result into a JSON format
     * @param Result $result
     * @return string
     */
    public function toJSON(Result $result) {
        return Json::encode(
            $result->toArray()
        );
    }


}